package com.platform.service;

import com.platform.dao.ApiAmountBillMapper;
import com.platform.dao.ApiUserMapper;
import com.platform.entity.ApiAmountBillVo;
import com.platform.entity.UserVo;
import com.platform.util.UtilServiceName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Service接口
 *
 * @author zhurusen
 * @email 524227263@qq.com
 * @date 2018-05-11 23:06:40
 */
@Service
public class ApiAmountBillService {

    @Autowired
    private ApiAmountBillMapper apiAmountBillMapper;
    @Autowired
    private ApiUserMapper apiUserMapper;

    public void save(ApiAmountBillVo billVo) {
        apiAmountBillMapper.save(billVo);
    }

    public List<ApiAmountBillVo> queryList(Map params) {
        return apiAmountBillMapper.queryList(params);
    }

    /**
     * 充值
     */
    public void recharge(ApiAmountBillVo billVo) {
        UserVo userVo = apiUserMapper.queryObject(billVo.getUserId());
        userVo.setAvailable_amount(userVo.getAvailable_amount().add(billVo.getAmount()));
        userVo.setWithdraw_amount(userVo.getWithdraw_amount().add(billVo.getAmount()));
        apiUserMapper.update(userVo);

    }


    public void connection(Long mouldId, BigDecimal amount, Long cmUserId) {
        /**
         * 收益的人
         */
        UserVo cmUser = apiUserMapper.queryObject(mouldId);
        cmUser.setAvailable_amount(cmUser.getAvailable_amount().add(amount));
        cmUser.setWithdraw_amount(cmUser.getWithdraw_amount().add(amount));

        cmUser.setPresent_amount(cmUser.getPresent_amount().add(amount));
        apiUserMapper.update(cmUser);

        /**
         * 插入流水记录
         */
        ApiAmountBillVo billVo = new ApiAmountBillVo();
        billVo.setCreateTime(new Date());
        billVo.setType(2);
        billVo.setMouldId(cmUserId);
        billVo.setMouldName("connection");
        billVo.setTitle("人脉收益");
        billVo.setUserId(mouldId);
        billVo.setAmount(amount);
        apiAmountBillMapper.save(billVo);
    }

    public void orderConsume(String serialNumber, Long mouldId, String mouldName, BigDecimal amount, UserVo loginUser) {

        /**
         * 扣钱
         */
        loginUser.setAvailable_amount(loginUser.getAvailable_amount().subtract(amount));

        BigDecimal presentAmount = new BigDecimal(0);
        if (loginUser.getPresent_amount().compareTo(amount) >= 0) {
            presentAmount = loginUser.getPresent_amount().subtract(amount);
        }
        loginUser.setPresent_amount(presentAmount);

        apiUserMapper.update(loginUser);
        /**
         * 支出 001
         */
        ApiAmountBillVo billVo = new ApiAmountBillVo();
        billVo.setBillNo(serialNumber);
        billVo.setCreateTime(new Date());
        billVo.setType(1);
        billVo.setMouldId(mouldId);
        billVo.setMouldName(mouldName);
        billVo.setTitle(UtilServiceName.serviceNmae(mouldName));
        billVo.setUserId(loginUser.getId());
        billVo.setAmount(amount);
        apiAmountBillMapper.save(billVo);
    }

    /**
     * 订单 结算
     *
     * @param mouldId   收益的人
     * @param mouldName 1 live 2 wake 3 emotion 4 guide 5 circleReward 6 videoReward 7 videoPay 8 indexReward  9 wxPay 10 alipay 11 connection
     * @param amount
     */
    public void orderConsumeEnd(Long mouldId, String mouldName, BigDecimal amount, UserVo loginUser) {


        /**
         * 产生了消费，人脉获得 5% 的收益
         */
        if (loginUser.getConnection() != null) {
            connection(loginUser.getConnection(), amount.multiply(new BigDecimal(0.05)), loginUser.getId());
        }


        /**
         * 主播收钱
         */
        UserVo cmUser = apiUserMapper.queryObject(mouldId);//收益的人

        cmUser.setAvailable_amount(cmUser.getAvailable_amount().add(amount.multiply(new BigDecimal(0.5))));
        cmUser.setWithdraw_amount(cmUser.getWithdraw_amount().add(amount.multiply(new BigDecimal(0.5))));
        cmUser.setPresent_amount(cmUser.getPresent_amount().add(amount.multiply(new BigDecimal(0.5))));
        apiUserMapper.update(cmUser);

        /**
         * 主播产生收益的话 ， 邀请的人 获取 10%
         */
        if (cmUser.getConnection() != null) {
            connection(cmUser.getConnection(), amount.multiply(new BigDecimal(0.1)), cmUser.getId());
        }


        ApiAmountBillVo billVo_SR = new ApiAmountBillVo();
        billVo_SR.setCreateTime(new Date());
        billVo_SR.setType(2);
        billVo_SR.setMouldId(loginUser.getId());
        billVo_SR.setMouldName(mouldName);
        billVo_SR.setTitle(UtilServiceName.serviceNmae(mouldName));
        billVo_SR.setUserId(mouldId);
        billVo_SR.setAmount(amount.multiply(new BigDecimal(0.5)));
        apiAmountBillMapper.save(billVo_SR);
    }


    /**
     * @param mouldId   收益的人
     * @param mouldName 1 live 2 wake 3 emotion 4 guide 5 circleReward 6 videoReward 7 videoPay 8 indexReward  9 wxPay 10 alipay 11 connection
     * @param amount
     */
    public void consume(Long mouldId, String mouldName, BigDecimal amount, UserVo loginUser) {

        /**
         * 收入
         */
        UserVo cmUser = apiUserMapper.queryObject(mouldId);

        if (!mouldName.equals("live")) {

            loginUser.setAvailable_amount(loginUser.getAvailable_amount().subtract(amount));

            BigDecimal presentAmount = new BigDecimal(0);
            if (loginUser.getPresent_amount().compareTo(amount) >= 0) {
                presentAmount = loginUser.getPresent_amount().subtract(amount);
            }

            loginUser.setPresent_amount(presentAmount);
            apiUserMapper.update(loginUser);

            if (loginUser.getConnection() != null) {
                connection(loginUser.getConnection(), amount.multiply(new BigDecimal(0.05)), loginUser.getId());
            }

            cmUser = apiUserMapper.queryObject(mouldId);

            cmUser.setAvailable_amount(cmUser.getAvailable_amount().add(amount.multiply(new BigDecimal(0.5))));
            cmUser.setWithdraw_amount(cmUser.getWithdraw_amount().add(amount.multiply(new BigDecimal(0.5))));
            cmUser.setPresent_amount(cmUser.getPresent_amount().add(amount.multiply(new BigDecimal(0.5))));
            apiUserMapper.update(cmUser);

            /**
             * 主播产生收益的话 ， 邀请的人 获取 10%
             */
            if (cmUser.getConnection() != null) {
                connection(cmUser.getConnection(), amount.multiply(new BigDecimal(0.1)), cmUser.getId());
            }

        }

        /**
         * 支出 001
         */
        ApiAmountBillVo billVo = new ApiAmountBillVo();
        billVo.setCreateTime(new Date());
        billVo.setType(1);
        billVo.setMouldId(mouldId);
        billVo.setMouldName(mouldName);
        billVo.setTitle(UtilServiceName.serviceNmae(mouldName));
        billVo.setUserId(loginUser.getId());
        billVo.setAmount(amount);
        apiAmountBillMapper.save(billVo);


        ApiAmountBillVo billVo_SR = new ApiAmountBillVo();
        billVo_SR.setCreateTime(new Date());
        billVo_SR.setType(2);
        billVo_SR.setMouldId(loginUser.getId());
        billVo_SR.setMouldName(billVo.getMouldName());
        billVo_SR.setTitle(billVo.getTitle());
        billVo_SR.setUserId(mouldId);
        billVo_SR.setAmount(amount.multiply(new BigDecimal(0.5)));
        apiAmountBillMapper.save(billVo_SR);


    }

    public ApiAmountBillVo selecyByBillNo(String billNo) {
        Map<String, Object> map = new HashMap<>();
        map.put("billNo", billNo);

        List<ApiAmountBillVo> list = apiAmountBillMapper.queryList(map);

        return list.size() > 0 ? list.get(0) : null;
    }

    public void updata(ApiAmountBillVo apiAmountBillVo) {
        apiAmountBillMapper.update(apiAmountBillVo);
    }

}
